﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections.Generic;
using System.Linq;

namespace Pipra.Uom.Imperial
{
    public class ImperialAreaAuthority : IUomAuthority
    {

        private static readonly ConversionFactorEntry EntryAcreToSquareFoot = new ConversionFactorEntry(43560);
        private static readonly ConversionFactorEntry EntrySquareFootToAcre = ConversionFactorEntry.GetInverted(EntryAcreToSquareFoot);

        private readonly ImperialLengthAuthority _lengthAuthority;

        private readonly Uom UomAcre;
        private readonly Uom UomSurveyAcre;

        private readonly Uom[] AllUoms;

        private readonly Uom UomSquareFoot;
        private readonly Uom UomSquareMetre;
        private readonly Uom UomSquareSurveyFoot;

        public ImperialAreaAuthority(ImperialLengthAuthority lengthAuthority)
        {
            _lengthAuthority = lengthAuthority;
            UomAcre = new Uom(NameConstants.NameAcre,this);
            UomSurveyAcre = new Uom(NameConstants.NameSurveyAcre,this);
            AllUoms =
                lengthAuthority.AllUoms
                .Select(n => new Uom(String.Concat(NameConstants.DimensionSquarePrefixSpace, n.Name),this))
                .Concat(new[] { UomAcre, UomSurveyAcre })
                .ToArray()
            ;
            UomSquareFoot =
                AllUoms.First(uom => uom.Name.Equals(NameConstants.NameSquareFoot));
            UomSquareSurveyFoot =
                AllUoms.First(uom => uom.Name.Equals(NameConstants.NameSquareSurveyFoot));
            UomSquareMetre =
                AllUoms.First(uom => uom.Name.Equals(NameConstants.NameSquareMetre));
        }

        public string Type
        {
            get { return NameConstants.NameArea; }
        }

        public IEnumerable<IUom> Units
        {
            get { return AllUoms.AsEnumerable().Cast<IUom>(); }
        }

        public IUom GetUnit(string name)
        {
            return AllUoms.FirstOrDefault(uom => Comparer.Equals(name, uom.Name));
        }

        public UomComparer Comparer
        {
            get { return _lengthAuthority.Comparer; }
        }

        public IEnumerable<IUom> GetMappedUnits(IUom from)
        {
            if (Comparer.Equals(from, UomAcre))
            {
                return new[] { UomSquareFoot };
            }
            if (Comparer.Equals(from, UomSurveyAcre))
            {
                return new[] { UomSquareSurveyFoot };
            }
            IEnumerable<IUom> result = (
                (Comparer.Equals(from, UomSquareFoot))
                ? new[] { UomAcre }
                : (
                    (Comparer.Equals(from, UomSquareSurveyFoot))
                    ? new[] { UomSurveyAcre }
                    : Enumerable.Empty<IUom>()
                )
            );
            string fromId = from.Name;
            if (
                fromId.Length > NameConstants.DimensionSquarePrefixSpace.Length
                && Comparer.Equals(NameConstants.DimensionSquarePrefixSpace, fromId.Substring(0, NameConstants.DimensionSquarePrefixSpace.Length))
            )
            {
                IUom lengthUnit = _lengthAuthority.GetUnit(
                    fromId.Substring(NameConstants.DimensionSquarePrefixSpace.Length)
                );
                return _lengthAuthority
                    .GetMappedUnits(lengthUnit)
                    .Select(n => String.Concat(NameConstants.DimensionSquarePrefixSpace, n))
                    .Select(n => AllUoms.First(u => Comparer.Equals(u.Name, n)))
                    .Cast<IUom>()
                ;
            }
            return result;
        }

        public ConversionFactorEntry GetEntry(IUom from, IUom to)
        {
            if (Comparer.Equals(from, UomAcre))
            {
                if (Comparer.Equals(to, UomSquareFoot))
                {
                    return EntryAcreToSquareFoot;
                }
            }
            else if (Comparer.Equals(from, UomSurveyAcre))
            {
                if (Comparer.Equals(to, UomSquareSurveyFoot))
                {
                    return EntryAcreToSquareFoot;
                }
            }
            else
            {
                if (Comparer.Equals(from, UomSquareFoot))
                {
                    if (Comparer.Equals(to, UomAcre))
                    {
                        return EntrySquareFootToAcre;
                    }
                }
                else if (Comparer.Equals(from, UomSquareSurveyFoot))
                {
                    if (Comparer.Equals(to, UomSurveyAcre))
                    {
                        return EntrySquareFootToAcre;
                    }
                }

                string fromId = from.Name;
                string toId = to.Name;

                if (
                    fromId.Length > NameConstants.DimensionSquarePrefixSpace.Length
                    && toId.Length > NameConstants.DimensionSquarePrefixSpace.Length
                    && Comparer.Equals(NameConstants.DimensionSquarePrefixSpace, fromId.Substring(0, NameConstants.DimensionSquarePrefixSpace.Length))
                    && Comparer.Equals(NameConstants.DimensionSquarePrefixSpace, toId.Substring(0, NameConstants.DimensionSquarePrefixSpace.Length))
                )
                {
                    ConversionFactorEntry lengthEntry = _lengthAuthority.GetEntry(
                        _lengthAuthority.GetUnit(fromId.Substring(NameConstants.DimensionSquarePrefixSpace.Length)),
                        _lengthAuthority.GetUnit(toId.Substring(NameConstants.DimensionSquarePrefixSpace.Length))
                    );
                    if (null != lengthEntry)
                    {
                        return ConversionFactorEntry.GetSquared(lengthEntry);
                    }
                }
            }
            return null;
        }

        public IValueConverter GetConverter(IUom from, IUom to)
        {
            return FactorValueConverter.CreateConverter(Type, from, to, GetEntry(from, to));
        }

    }
}
